let host = "47.98.143.246";
let appID = "wxa690edcf92ee8f2e";
let appSecret = '7d4c2f2b7d014983cf1db8c9f34ac1e1';
let code;
let wechatUrlName = 'yckz003.top';
//set cookie
function setCookie(key, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = key + "=" + escape(value) + ";expires=" + exdate.toGMTString() + ";path=/";
}
// 读取cookie
function getCookie(key)
{
    var arr,reg=new RegExp("(^| )"+key+"=([^;]*)(;|$)");
    if(arr===document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
//删除cookies
function deleteCookie(key) {
    setCookie(key, "", -1);
}
// 检查cookie
function checkCookie(key) {
    username = getCookie(key);
    console.log(username);
    return username !== null && username !== "";
}
let openIdCookieKey = 'openid_cookie';
function getOpenIdSession() {
    if (checkCookie(openIdCookieKey)) {
        let cookieValue = getCookie(openIdCookieKey);
        let url = 'http://' + host + "/user_account/check_token?token" + cookieValue;
        let response = httpGet(url);
        let result = JSON.parse(response);
        if (result.status === 1) {
            return result.data.user_id;
        }
    }
}

function openIdUpload() {
    let result = getOpenIdSession();
    if (result === "") {
        if (setCookie("code")) {
            code = getCookie("code");
            let openID = getUserOpenId()["openid"];
            if (openID) {
                return setOpenId(openID);
            } else {
                return false;
            }
        } else {
            setCookie(openIdCookieKey, getCookie(openIdCookieKey), 0.5);
            return true;
        }
    }
}
//向服务器上传openid
function setOpenId(openid) {
    let url = "http://" + host + "/user_account/login_by_wechat?openid=" + openid;
    let res = JSON.parse(httpGet(url));
    if (res.status === "1") {
        setCookie(openIdCookieKey, res.data.token, 0.5);
        return true;
    } else {
        return false;
    }
}

function getUserOpenId() {
    let result = getRefreshAccessToken();
    let url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + result.access_token + "&openid=" + result.openid + "&lang=zh_CN";
    let json = JSON.parse(httpGet(url));
    let str = new Map();
    if (json.openid){
        str['openid'] = json.openid;
        return str;
    } else {
        str['openid'] = null;
        return str;
    }
}

function getRefreshAccessToken() {
    let url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appID + "&secret=" + appSecret + "&code=" + code;
    url = url + "&grant_type=authorization_code";
    return JSON.parse(httpGet(url));
}
function httpGet(url) {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {
            return xhr.responseText;
        }
    };
    xhr.open("GET", url, true);
    xhr.send();
}

function httpPost(url, data) {
    let xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.setRequestHeader('Content-type', 'application/json;');
    xhr.send(JSON.stringify(data));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304 )) {
            return xhr.responseText;
        }
    }
}
